<?php
/**
 * Created by PhpStorm.
 * User: rain
 * Date: 2018/4/5
 * Time: 下午4:51
 */

namespace App\Http\Controllers\Qiniu;


use App\Http\Controllers\Controller;
use App\Utils\QiniuUtil;
use Illuminate\Http\Request;
use Ramsey\Uuid\Uuid;

class TokenController extends Controller
{
    private $qiniuUtil;

    public function __construct(QiniuUtil $qiniuUtil)
    {
        $this->qiniuUtil = $qiniuUtil;
    }

    public function url()
    {
        $key = substr($this->qiniuUtil->getFilePath('uploads', Uuid::uuid1(), 'jpg'), 1);

        return ['uptoken' => $this->qiniuUtil->getUploadToken(), 'key' => $key];
    }

    public function uploadFile(Request $request)
    {
        $file = $request->file('file');

        $url = $this->qiniuUtil->uploadFile($file, $request->get('type', 'uploads'));

        return $this->success(['url' => $url]);
    }

}